package org.dstart.draw.cui.symbol.basic
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	
	import org.dstart.draw.cglobal.GColors;
	import org.dstart.draw.cglobal.GValue;
	import org.dstart.draw.csuper.CSprite;
	import org.dstart.draw.cui.symbol.basic.iteam.BWinTitleButton;
	import org.dstart.draw.cui.symbol.basic.iteam.BWinTitleButtonModel;
	import org.dstart.draw.cui.symbol.basic.iteam.BWinTitleIcon;
	import org.dstart.draw.cui.symbol.events.CWinTitleEvent;
	import org.dstart.draw.cutils.CGraphics;
	import org.dstart.draw.cutils.CGraphicsRoundModel;
	import org.dstart.draw.iface.IDraw;

	public class BWindowTitle extends CSprite implements IDraw
	{
		private var m_width:Number;		//	基础宽度
		private var m_height:Number;	//	基础高度
		
		private var initFlag:Boolean;	//	是否初始化
		

		public function BWindowTitle(w:Number = 300)
		{
			InitObject();
			ChangeSize(w, 26);				
		}
		
		public function ChangeSize(ww:Number, hh:Number):void
		{
			this.m_width = ww;
			this.m_height = hh;
			
			if(initFlag)
			{
				RushObject();
			}
		}
		
		//	取得对象的大小
		public function get rect():Rectangle
		{
			return new Rectangle(0, 0, m_width, m_height);
		}
		
		//	读取是否最大化
		public function get isMaximize():Boolean
		{
			return Boolean(!this.btnMaximize.visible && this.btnMinimize);
		}
		
		//	读取是否最小化
		public function get isMinimize():Boolean
		{
			return !this.btnMinimize.visible;
		}

		//-------------------------------------------------------------
		
		private var sprMain:Sprite;			//	主区块
		private var sprIcon:BWinTitleIcon;	//	标题图标
		private var sprText:TextField;		//	标题文字
		
		private var btnMinimize:BWinTitleButton;	//	最小化按钮
		private var btnMaximize:BWinTitleButton;	//	最大化按钮
		private var btnClose:BWinTitleButton;		//	关闭按钮
		private var btnRestore:BWinTitleButton;		//	还原按钮
		private var btnHelp:BWinTitleButton;		//	帮助按钮
		
		private function InitObject():void
		{
			initFlag = true;
						
			sprMain = new Sprite();
			
			sprIcon = new BWinTitleIcon();
			sprText = new TextField();			
			sprText.text = "CFrame";
			sprText.width = sprText.textWidth + 6;
			sprText.height = sprText.textHeight + 4;
			sprText.selectable = false;
			
			btnMinimize = new BWinTitleButton(BWinTitleButtonModel.MINIMIZE);
			btnMaximize = new BWinTitleButton(BWinTitleButtonModel.MAXIMIZE);
			btnClose = new BWinTitleButton(BWinTitleButtonModel.CLOSE);
			btnRestore = new BWinTitleButton(BWinTitleButtonModel.RESTORE);
			
			btnRestore.visible = false;
			btnMaximize.visible = true;
			
			_root.addChild(sprMain);
			_root.addChild(sprIcon);
			_root.addChild(sprText);
			_root.addChild(btnMinimize);
			_root.addChild(btnMaximize);
			_root.addChild(btnRestore);
			_root.addChild(btnClose);			
		}			
		
		private function RushObject():void
		{
			var lc:uint = GColors.WIN_BG_LINE_COLOR;
			var fhc:uint = GColors.WIN_BG_HFILL_COLOR;
			var flc:uint = GColors.WIN_BG_LFILL_COLOR;
			var r:Number = GValue.DEF_ROUND_SIZE;
			
			sprMain.graphics.clear();
			
			var tc:CGraphics = new CGraphics(sprMain.graphics, lc, fhc, flc);
			tc.DrawRectangle(new Rectangle(0, 0, m_width, m_height), r - 3, CGraphicsRoundModel.UP_ROUND, true, true, 90);			
		
			sprIcon.x = 0;
			sprIcon.y = 0;
			
			sprText.x = sprIcon.x + sprIcon.width + 10;
			sprText.y = (m_height - sprText.height)/2;
			
			this.btnClose.x = m_width - this.btnClose.width - 5;
			this.btnClose.y = (m_height - this.btnClose.height)/2;
			
			this.btnMaximize.x = this.btnClose.x - this.btnMaximize.width;
			this.btnMaximize.y = this.btnClose.y;
			
			this.btnRestore.x = this.btnMaximize.x;
			this.btnRestore.y = this.btnMaximize.y;			
			
			this.btnMinimize.x = this.btnMaximize.x - this.btnMaximize.width;
			this.btnMinimize.y = this.btnMaximize.y;
		}
		
		protected override function InitDraw():void
		{
			this.sprIcon.mouseChildren = this.sprIcon.mouseEnabled = false;
			this.sprText.mouseEnabled = false;
			
			//	控制按钮事件
			this.btnMaximize.addEventListener(MouseEvent.CLICK, onBtnMaxClick);
			this.btnMinimize.addEventListener(MouseEvent.CLICK, onBtnMinClick);
			this.btnClose.addEventListener(MouseEvent.CLICK, onBtnCloseClick);
			this.btnRestore.addEventListener(MouseEvent.CLICK, onBtnRestoreClick);
			
			//	背景条事件
			sprMain.doubleClickEnabled = true;			
			sprMain.addEventListener(MouseEvent.MOUSE_DOWN, onStartDragEvent);
			sprMain.addEventListener(MouseEvent.MOUSE_UP, onStopDragEvent);
			sprMain.addEventListener(MouseEvent.DOUBLE_CLICK, onDBClick);					
		}

		//	通知外部，开始拖动窗口
		private function onStartDragEvent(e:MouseEvent):void
		{
			if(!this.isMaximize)
			{
				SendMsg(CWinTitleEvent.START_MOVE);
				
				stage.addEventListener(MouseEvent.MOUSE_MOVE, onRushFrame);
				stage.addEventListener(MouseEvent.MOUSE_UP, onStopDragEvent);
			}
		}
		
		//	通知外部，停止拖动窗口
		private function onStopDragEvent(e:MouseEvent):void
		{
			if(!this.isMaximize)
			{
				SendMsg(CWinTitleEvent.STOP_MOVE);
				
				stage.removeEventListener(MouseEvent.MOUSE_MOVE, onRushFrame);
				stage.removeEventListener(MouseEvent.MOUSE_UP, onStopDragEvent);
			}
		}
		
		//	刷新帧画面
		private function onRushFrame(e:MouseEvent):void
		{
			SendMsg(CWinTitleEvent.MOVE_PRESS);
			
			e.updateAfterEvent();
		}
		
		//	点击 最大化 按钮
		private function onBtnMaxClick(e:MouseEvent):void
		{
			this.btnMaximize.visible = false;
			this.btnRestore.visible = true;			
			
			SendMsg(CWinTitleEvent.MAXIMIZE);
		}
		
		//	点击 还原 按钮
		private function onBtnRestoreClick(e:MouseEvent):void
		{
			this.btnMaximize.visible = true;
			this.btnMinimize.visible = true;
			this.btnRestore.visible = false;				
			
			SendMsg(CWinTitleEvent.RESTORE);
		}
		
		//	点击 最小化 按钮
		private function onBtnMinClick(e:MouseEvent):void
		{
			this.btnMaximize.visible = false;
			this.btnMinimize.visible = true;
			this.btnRestore.visible = true;			
			
			SendMsg(CWinTitleEvent.MINIMIZE);
		}
		
		//	点击 关闭 按钮
		private function onBtnCloseClick(e:MouseEvent):void
		{
			SendMsg(CWinTitleEvent.CLOSE);
		}
		
		//	双击时触发
		private function onDBClick(e:MouseEvent):void
		{
			if(isMaximize || isMinimize)
			{
				onBtnRestoreClick(e);
			}
			else
			{
				onBtnMaxClick(e);
			}
		}
		
		//	发送消息
		private function SendMsg(str:String):void
		{
			this.dispatchEvent(new CWinTitleEvent(str));
		}
	}
}